Page({
  data: {
    selected1: true,
    selected2: false,
    selected3: false,
    array: ['黎明社区卫生服务中心', '星光社区卫生服务中心'],
    agencyArr:[],
    agencyIdArr:[],
    checkDateArr: [],
    arrayTime: ['请选择日期','2018-02-04', '2018-02-06'],
    index: 0,
    indextime: 0,
    datalistInfo: [],
    datalist: [],
    geneItem:'',
    geneReportSize:'',
    num:0,
    listAgencyAndCheckDate:'',
    medicalCategorys:'',
    swiperCurrent:0
  },

  onLoad: function () {
    var userInfo = wx.getStorageSync('trUserInfo');
    //基因报告信息
    var that = this;
    wx.request({
      url: getApp().globalData.host_url + '/gene/ajax_showGeneByUser?userId=' + userInfo.userId,
      method: 'GET',
      data: {},
      success: function (res) {
        var gene = res.data.gene;
        var geneReportInfos = res.data.geneReportInfos;
        if (gene != null) {
          that.setData({
            datalist: [
              {
                name: '项疾病风险高',
                num: gene.highRisk
              },
              {
                name: '项疾病风险较高',
                num: gene.higherRisk
              },
              {
                name: '项疾病风险一般',
                num: gene.commonlyRisk
              },
              {
                name: '项疾病风险较低',
                num: gene.lowerRisk
              },
              {
                name: '项疾病风险低',
                num: gene.lowRisk
              }
            ],
            geneItem: res.data.geneItem,
            geneReportSize: res.data.geneReportSize,
            datalistInfo: res.data.geneReportInfos
          })
        }
      }
    })

    wx.request({
      url: getApp().globalData.host_url + '/medicalExReport/ajax_viewMedicalExReport?userId=' + userInfo.userId,
      method: 'GET',
      data: {},
      success: function (res) {
        var listAgency = res.data.listAgency;
        var listAgencyAndCheckDate = res.data.listAgencyAndCheckDate;
        if (listAgency == null || listAgencyAndCheckDate== null) {
          var textToast = "信息获取失败！";
          wx.showToast({
            title: textToast,
            icon: '',
            duration: 2000
          })
        } else {
          var agencyNameArr = [];
          var agencyIdArr = [];
          var checkDateArr = [];
          
          for (var i = 0; i < listAgency.length; i++){
            agencyIdArr.push(listAgency[i][0]);
            agencyNameArr.push(listAgency[i][1]);  
          }
          for (var i = 0; i < listAgencyAndCheckDate.length; i++){
            if(listAgencyAndCheckDate[i][0] == listAgency[0][0]){
              checkDateArr.push(listAgencyAndCheckDate[i][2]);
            }   
          }
          //按照时间正序获取信息
          var listAgencyAndCheckDate_Array = [];
          for (var i = listAgencyAndCheckDate.length -1; i >= 0; i--) {
            listAgencyAndCheckDate_Array.push(listAgencyAndCheckDate[i]);
          }
		  
          that.setData({
            agencyArr: agencyNameArr,
            agencyIdArr: agencyIdArr,
            listAgencyAndCheckDate: listAgencyAndCheckDate_Array,
            swiperCurrent: listAgencyAndCheckDate_Array.length - 1,
            checkDateArr: checkDateArr,
            num: listAgencyAndCheckDate_Array.length - 1
          })
          var agencyId = listAgencyAndCheckDate[0][0];
          var checkDate = listAgencyAndCheckDate[0][2];
          that.getCheckList(agencyId, checkDate);
        }
      }
    })
  },
  onShow: function () {
    
  },

  selected1: function (e) {
    this.setData({
      selected1: true,
      selected2: false,
      selected3: false
    })
  },
  selected2: function (e) {
    this.setData({
      selected1: false,
      selected2: true,
      selected3: false
    })
  },
  selected3: function (e) {
    this.setData({
      selected1: false,
      selected2: false,
      selected3: true
    })
    var userInfo = wx.getStorageSync('trUserInfo');
    //基因报告信息
    var that = this;
    wx.request({
      url: getApp().globalData.host_url + '/gene/ajax_showGeneByUser?userId=' + userInfo.userId,
      method: 'GET',
      data: {},
      success: function (res) {
        var gene = res.data.gene;
        var geneReportInfos = res.data.geneReportInfos;
        if (gene != null) {
          that.setData({
            datalist: [
              {
                name: '项疾病风险高',
                num: gene.highRisk
              },
              {
                name: '项疾病风险较高',
                num: gene.higherRisk
              },
              {
                name: '项疾病风险一般',
                num: gene.commonlyRisk
              },
              {
                name: '项疾病风险较低',
                num: gene.lowerRisk
              },
              {
                name: '项疾病风险低',
                num: gene.lowRisk
              }
            ],
            geneItem: res.data.geneItem,
            geneReportSize: res.data.geneReportSize,
            datalistInfo: res.data.geneReportInfos
          })
        }
      }
    })
  },
  bindPickerChangeOrg: function(e){
    var that = this;
    this.setData({
      index: e.detail.value
    })
    var agencyId = that.data.agencyIdArr[e.detail.value];
    var userInfo = wx.getStorageSync('trUserInfo');
    wx.request({
      url: getApp().globalData.host_url + '/medicalExReport/ajax_getAgencyCheckDate?agencyId=' + agencyId + '&userId=' + userInfo.userId,
      method: 'GET',
      data: {},
      success: function (res) {
        var agencyDates = res.data.agencyDates;
        if (agencyDates == null) {
          var textToast = "信息获取失败！";
          wx.showToast({
            title: textToast,
            icon: '',
            duration: 2000
          })
        } else {
          var checkDateArr = [];
          for (var i = 0; i < agencyDates.length; i++) {
            checkDateArr.push(agencyDates[i]);
          }
          that.setData({
            checkDateArr: checkDateArr
          })
        }
      }
    })
  },
  bindPickerChangeDate: function(e){
    var that = this;
    this.setData({
      indextime: e.detail.value
    })
    var agencyId = that.data.agencyIdArr[that.data.index];
    var checkDate = that.data.checkDateArr[that.data.indextime];
    var listAgencyAndCheckDate = that.data.listAgencyAndCheckDate;
    for (var i = 0; i < listAgencyAndCheckDate.length; i++) {
      if (agencyId == listAgencyAndCheckDate[i][0] && checkDate == that.data.listAgencyAndCheckDate[i][2]){
          that.setData({
            swiperCurrent: i
          })
          break;  
        }
    }
    that.getCheckList(agencyId, checkDate);  
  },
  bindSwiperChange: function (e) {
    var that = this;
    var current = e.detail.current;
    var source = e.detail.source;
    var listAgencyAndCheckDate = that.data.listAgencyAndCheckDate;
    if (current < listAgencyAndCheckDate.length){
      var agencyId = that.data.listAgencyAndCheckDate[current][0];
      var checkDate = that.data.listAgencyAndCheckDate[current][2];
      this.setData({
        num: current
      })
      that.getCheckList(agencyId, checkDate);

    }
  },
  bindCategoryView: function (e) {
    var that = this;
    
  },
  getCheckList: function (agencyId, checkDate) {
    var that = this;
    var userInfo = wx.getStorageSync('trUserInfo');
    wx.request({
      url: getApp().globalData.host_url + '/medicalExReport/ajax_getMedicalCategory?agencyId=' + agencyId + "&checkDate=" + checkDate + "&userId=" + userInfo.userId,
      method: 'GET',
      data: {},
      success: function (res) {
        var medicalCategorys = res.data.medicalCategorys;
        console.log(medicalCategorys);
        if (medicalCategorys == null) {
          var textToast = "信息获取失败！";
          wx.showToast({
            title: textToast,
            icon: '',
            duration: 2000
          })
        } else {
          that.setData({
            medicalCategorys: medicalCategorys
          })
        }
      }
    })

  },
  onItemClick: function (event) {
    var userInfo = wx.getStorageSync('trUserInfo');
    var agency = event.target.dataset.agency;
    if (agency == 'uploadImg'){
      wx.chooseImage({
        count: 1, // 默认9
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
        success: function (res) {
          // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
          var tempFilePaths = res.tempFilePaths;
          const uploadTask = wx.uploadFile({
            url: getApp().globalData.host_url + '/medicalExReport/ajax_uploadImg',      //此处换上你的接口地址
            filePath: tempFilePaths[0],
            name: 'img',
            header: {
              "Content-Type": "multipart/form-data",
              'accept': 'application/json'
            },
            formData: {
              'userId': userInfo.userId  //其他额外的formdata，可不写
            },
            success: function (res) {
              console.log(res.data);
              wx.showToast({

                title: '报告上传成功！',//这里打印出报告上传成功

                icon: 'success',

                duration: 1000

              })
            },
            fail: function (res) {
              console.log(res.data);

            },
          })

          uploadTask.onProgressUpdate((res) => {
            console.log('上传进度', res.progress)
            console.log('已经上传的数据长度', res.totalBytesSent)
            console.log('预期需要上传的数据总长度', res.totalBytesExpectedToSend)
          })
         
        }
      })
    }

  }

})
